<?php


namespace App\Services;


use App\Models\Config;
use App\Models\MemberOther;
use App\Models\MemberQnRisk;
use App\Models\Order;
use App\Models\SaleUser;
use PhpOffice\PhpWord\TemplateProcessor;

class RiskQnypService
{
    public static $connectTimeout = 30;//30 second
    public static $readTimeout = 80;//80 second
//    const IDURL = 'api/accurate-risk/nuom';
//    const HOST = 'https://alchemist-api.qnvipmall.com/';
//    const ProjectId = '诺明网络';
//    const ProjectScert = 'nuom-2024-im0i4fh5';

//    public static function query($post = [])
//    {
//        $array = [];
////        $array['baseInfo']=[
////            'loanId'=>$post['trade_no'],
////            'name'=>$post['cert_name'],
////            'mobile'=>$post['mobile'],
////            'idcardNo'=>$post['cert_no'],
////            'channel'=>$post['channel'],
////            'consignee'=>$post['cert_name'],
////            'consigneeMobile'=>$post['address_mobile'],
////            'consigneeAddress'=>$post['address'],
////            'idCardAddress'=>$post['idCardAddress'],
////        ];
//        $one = MemberQnRisk::where('uid', $post['uid'])->orderBy('id', 'desc')->first();
//        if (!empty(obToArray($one))) {
//            return true;
//        }
//        $link = self::licenseUrl($post);
//        if (!$link) {
//            return false;
//        }
//        $channel_name = SaleUser::where('sale_user_id', $post['sale_user_id'])->value('channel_name');
//        $array = [
//            'name' => $post['cert_name'],
//            'mobile' => $post['mobile'],
//            'idCardNo' => $post['cert_no'],
//            'channel' => 'baabb46a0ccf0d2917f4e0b78b05e89d',
//            'encryptMode' => 0,
//            'flowNo' => time() . $post['uid'],
//            'personContractFileUrl' => $link,
//            'bizData' => ['quotientName' => $channel_name ? $channel_name : '其他']
//        ];
//        $arraya = $array;
//        $sign = 'Basic ' . base64_encode(self::ProjectId . ':' . self::ProjectScert);
//        $array = json_encode($array);
//        $res = http_post(self::HOST . self::IDURL, $array, array('Content-Type:application/json', 'sign:' . $sign));
//
//        $res = json_decode($res, true);
//        if ($res['code'] == 0) {
//            MemberQnRisk::insertFlowMember($post['cert_no'], $post['cert_name'], $post['uid'], $arraya['flowNo']);
//        }
//
//        self::addLog('青年优品风控查询', $array, $res);
//        return $res;
//    }


//    public static function licenseUrl($order)
//    {
//        set_time_limit(0);
//        ini_set('memory_limit', '-1');
////        $order = Order::from('sp_order_2 as o')->where('o.id',$orderId)
////            ->leftjoin('sp_member_2 as m','m.id','o.uid')
////            ->select(
////                'o.id','o.uid','o.mobile as user_mobile','o.pact_url_5','o.trade_no'
////                ,'m.cert_name','m.cert_no'
////                ,'m.mobile','m.esign_user_id','m.esign_seal_id','m.user_id','m.idcard_front','m.idcard_back'
////            )
////            ->first();
//        if (empty($order['esign_user_id'])) {
//            $result = EsignService::addPersonAccountID($order['cert_no'], $order['cert_name'], $order['cert_no'], $order['user_mobile']);
//            $data1 = $result['data'];
//            if (!empty($data1['accountId'])) {
//                $esign_user_id = $data1['accountId'];
//                MemberOther::where('id', $order['uid'])->update(['esign_user_id' => $esign_user_id]);
//            } else {
//                return false;
//            }
//        } else {
//            $user_mobile = !empty($order['user_mobile']) ? $order['user_mobile'] : $order['mobile'];
//            $esign_user_id = EsignService::updateAccounts($order['esign_user_id'], $user_mobile, $order['cert_name']);
//            if (empty($esign_user_id)) {
//                $result = EsignService::addPersonAccountID($order['cert_no'], $order['cert_name'], $order['cert_no'], $order['user_mobile']);
//                $data1 = $result['data'];
//                if (!empty($data1['accountId'])) {
//                    $esign_user_id = $data1['accountId'];
//                    MemberOther::where(['id' => $order['uid']])->update(['esign_user_id' => $esign_user_id]);
//                    $order['esign_user_id'] = $esign_user_id;
//                } else {
//                    return false;
//                }
//            }
//        }
//
//
//        $result = EsignService::createQnRiskUserAuth($order);
//
//        $flow_id = $result['data']['flowId'];
////        var_dump($flow_id);
//
////        var_dump($flow_id);
//        for ($i = 0; $i < 20; $i++) {
//            sleep(2);
//            $return_content = EsignService::downloadDocument($flow_id);
//            if (!isset($return_content['fileUrl']) || empty($return_content['fileUrl'])) {
//                sleep(2);
//            } else if ($return_content && !empty($return_content['fileUrl'])) {
//                break;
//            }
//        }
//        if ($return_content && !empty($return_content['fileUrl'])) {
//            $fileUrl = $return_content['fileUrl'];
//            $fileName = $flow_id . ".pdf";
//            $file = file_get_contents($fileUrl);
//            $path = dirname(dirname((dirname(__FILE__)))) . '/storage/tmp/' . $fileName;
//
//            file_put_contents($path, $file);
//            $uploud_res = AlipayService::upload($fileName, $path);
//            if ($uploud_res['status']) {
//                if (file_exists($path)) {
//                    unlink($path);
//                }
//            }
//
//            return config('alipay.oss.imgHost') . $fileName;
//        }
//
//    }

//    public static function addLog($message, $request = [], $response = [])
//    {
//        $content = [
//            'request' => is_object($request) ? object_array($request) : $request,
//            'response' => is_object($response) ? object_array($response) : $response,
//        ];
//        \Illuminate\Support\Facades\Log::debug($message, $content);
//    }


}
